import lib from '../../../assets/js/lib'
import qs from 'qs';
function download(url, fileName, reqData){
  let xhr = new XMLHttpRequest();
  xhr.open('POST', lib.config.host.system + '/common/download', true);
  // xhr.setRequestHeader('Content-Type', 'application/json');
  xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
  xhr.responseType = "blob";
  xhr.onload = function () {
    if (this.status === 200) {
      console.log(this.response);
      let blob = this.response;
      if (navigator.appVersion.toString().indexOf('.NET') > 0) {
        window.navigator.msSaveBlob(blob, fileName);
      } else {
        let body = document.body;
        let a = document.createElement('a');
        a.download = fileName;
        a.href = URL.createObjectURL(blob);
        body.appendChild(a);
        a.click();
        body.removeChild(a);
      }
    }
  };
  reqData = Object.assign({},reqData, {
    fileName: fileName,
    filePath: url
  });
  xhr.send(qs.stringify(reqData));
}
export default {download};
